<template>
  <div style="padding: 40px">

    <div style="margin: 10px 0;">
      <el-button type="primary"  v-if="userRole === 'admin' || userRole === 'teacher'" style="margin-left: 10px;" @click="add" >创建通知</el-button>
    </div>

    <el-table :data="tableData" border stripe style="width: 100%;"  header-align="center" align="center">
      <el-table-column label="通知ID" width="70px"  header-align="center" align="center">
        <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="title" label="通知消息" width="100px"  header-align="center" align="center"/>
      <el-table-column prop="content" label="通知内容"  header-align="center" align="center"/>
      <el-table-column prop="author" label="通知人姓名"  header-align="center" align="center"/>
      <el-table-column fixed="right" label="操作" width="180px"  header-align="center" align="center" v-if="userRole === 'admin' || userRole === 'teacher'">
        <template #default="scope">
          <!-- 编辑按钮  -->
          <el-button type="primary" v-if="userRole === 'admin' || userRole === 'teacher'"  @click="handleEdit(scope.row)">更新</el-button>

          <!-- 删除按钮 -->
          <el-popconfirm v-if="userRole === 'admin' " title="这段内容确定删除吗?" @onConfirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增弹窗 -->
    <el-dialog :visible.sync="dialogVisible" title="创建通知消息" width="30%">
      <el-form :model="form" label-width="120px">
        <el-form-item label="通知消息">
          <el-input v-model="form.title" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="通知内容">
          <el-input v-model="form.content" style="width: 50%"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 点击编辑的弹窗 -->
    <el-dialog :visible.sync="dialogVisible1" title="更新通知" width="30%">

      <el-form :model="form" label-width="120px">
        <el-form-item label="通知ID">
          <el-input v-model="form.id" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="通知消息">
          <el-input v-model="form.title" style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="通知内容">
          <el-input v-model="form.content" style="width: 50%"></el-input>
        </el-form-item>
      </el-form>


      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取消</el-button>
        <el-button type="primary" @click="saveEdit">确定</el-button>
      </span>

    </el-dialog>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total,prev, pager, next, jumper"
        :total="total">
    </el-pagination>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {
  getGuanliyuanDelete,
  getGuanliHuoQuTongzhi,
  getGuanliCreatTongzhi,
  getGuanliYuanUpDateXiao
} from '@/http/userList.js'
import {getStudentAllTongZhi} from "@/http/categories";

export default {
  name: "Notice",

  data() {
    return {
      form: {
        "id":"",
        "content": "",
        "title": ""
      },


      dialogVisible: false,
      dialogVisible1: false,
      dialogVisible2: false,
      currentPage: 1,
      pageSize: 10,
      total:0,
      tableData: [],

    }

  },
  created() {
    this.load()
  },

  methods: {

    async load() {
      const { code, message, data } = await getStudentAllTongZhi({current: this.currentPage, size: this.pageSize})
      console.log(data)
      this.tableData = data.records
      this.total= Number(data.total)

    },
    add() {
      console.log("123");
      this.dialogVisible = true
      this.form = {
        "content": "",
        "title": ""
      }
    },
    // 确认添加
    async save() {
      const { code, message } = await getGuanliCreatTongzhi(this.form)
      console.log(this.form)
      if (code == '0') {
        this.$message({
          type: "success",
          message: "添加成功"
        })
      } else {
        this.$message({
          type: "error",
          message: "添加失败"
        })
      }
      this.dialogVisible = false
      this.load()
    },
    // 编辑
    handleEdit(row) {
      console.log(row);
      this.form = { ...row }
      this.dialogVisible1 = true
    },
    // 确认修改
    async saveEdit() {

      const { code, message } = await getGuanliYuanUpDateXiao(this.form)
      if (code == '0') {
        this.$message({
          type: "success",
          message: "修改成功"
        })
      } else {
        this.$message({
          type: "error",
          message: "修改失败"
        })
      }
      this.dialogVisible1 = false
      this.load()
    },
    async handleDelete(id) {

      const { code, message } = await getGuanliyuanDelete({ id: id})
      if (code == '0') {
        this.$message({
          type: "success",
          message: "删除成功"
        })
      } else {
        this.$message({
          type: "error",
          message: message
        })
      }
      this.load()  // 删除之后重新加载表格的数据
    },
    handleSizeChange(pageSize ) {
      this.pageSize = pageSize;
      this.currentPage = 1;
      this.load()
    },
    async handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      const { code, message, data } = await getStudentAllTongZhi({ current: pageNum, size: this.pageSize })
      console.log(data)
      this.tableData = data.records
      this.total= Number(data.total)
      this.load(); // 重新加载数据
    },
    onDaloag(row) {
      this.form1 = row
      this.dialogVisible2 = true
    }
  },
  computed: {
    ...mapGetters({
      userRole: 'user/getUserRole'
    })
  }

}
</script>

<style></style>
